#!/usr/bin/python3

import os
import re
import sys
import uuid
import pathlib
import strict_winbcd

# obj = strict_winbcd.BCD("/usr/lib/python3.13/site-packages/strict_winbcd/BCD.minimal")

obj = strict_winbcd.BCD("/home/fpemud/test10/stage4-mswin_7/partition0.mntdir/Boot/BCD.bak")
a = sys.stdout

print(obj.get_boot_manager_info())


def mbrGetDiskAndPartitionWinBcdUuid(partDevPath):
    devPath, partId = "/home/fpemud/test10/stage4-mswin_7/disk.img", 1

    disk_signature = None
    start_sector_bytes = None
    with open(devPath, "rb") as f:
        # read MBR disk signature (4 bytes at offset 0x1B8)
        f.seek(0x1B8)
        disk_signature = f.read(4)

        # read partition start sector info directly from MBR
        f.seek(0x1BE + (partId - 1) * 16 + 8)  # jump to partition entry, offset to LBA field
        start_sector_bytes = f.read(4)
        startSector = int.from_bytes(start_sector_bytes, byteorder='little') * 512
        print(startSector)


    # convert to UUID: signature + 12 zero bytes
    diskUuid = uuid.UUID(bytes_le=disk_signature + b'\x00' * 12)

    # Convert to UUID: start sector to little-endian 4 bytes + 12 zero bytes
    partUuid = uuid.UUID(bytes_le=startSector.to_bytes(4, byteorder='little') + b'\x00' * 12)

    return (diskUuid, partUuid)


class PartiUtil:

    @staticmethod
    def isDiskOrParti(devPath):
        if re.fullmatch("/dev/sd[a-z]", devPath) is not None:
            return True
        if re.fullmatch("(/dev/sd[a-z])([0-9]+)", devPath) is not None:
            return False
        if re.fullmatch("/dev/xvd[a-z]", devPath) is not None:
            return True
        if re.fullmatch("(/dev/xvd[a-z])([0-9]+)", devPath) is not None:
            return False
        if re.fullmatch("/dev/vd[a-z]", devPath) is not None:
            return True
        if re.fullmatch("(/dev/vd[a-z])([0-9]+)", devPath) is not None:
            return False
        if re.fullmatch("/dev/nvme[0-9]+n[0-9]+", devPath) is not None:
            return True
        if re.fullmatch("(/dev/nvme[0-9]+n[0-9]+)p([0-9]+)", devPath) is not None:
            return False
        assert False

    @staticmethod
    def partiToDiskAndPartiId(partitionDevPath):
        m = re.fullmatch("(/dev/sd[a-z])([0-9]+)", partitionDevPath)
        if m is not None:
            return (m.group(1), int(m.group(2)))
        m = re.fullmatch("(/dev/xvd[a-z])([0-9]+)", partitionDevPath)
        if m is not None:
            return (m.group(1), int(m.group(2)))
        m = re.fullmatch("(/dev/vd[a-z])([0-9]+)", partitionDevPath)
        if m is not None:
            return (m.group(1), int(m.group(2)))
        m = re.fullmatch("(/dev/nvme[0-9]+n[0-9]+)p([0-9]+)", partitionDevPath)
        if m is not None:
            return (m.group(1), int(m.group(2)))
        assert False

    @staticmethod
    def partiToDisk(partitionDevPath):
        return PartiUtil.partiToDiskAndPartiId(partitionDevPath)[0]

    @staticmethod
    def diskToParti(diskDevPath, partitionId):
        m = re.fullmatch("/dev/sd[a-z]", diskDevPath)
        if m is not None:
            return diskDevPath + str(partitionId)
        m = re.fullmatch("/dev/xvd[a-z]", diskDevPath)
        if m is not None:
            return diskDevPath + str(partitionId)
        m = re.fullmatch("/dev/vd[a-z]", diskDevPath)
        if m is not None:
            return diskDevPath + str(partitionId)
        m = re.fullmatch("/dev/nvme[0-9]+n[0-9]+", diskDevPath)
        if m is not None:
            return diskDevPath + "p" + str(partitionId)
        assert False

    @staticmethod
    def diskHasParti(diskDevPath, partitionId):
        partiDevPath = PartiUtil.diskToParti(diskDevPath, partitionId)
        return os.path.exists(partiDevPath)

    @staticmethod
    def diskHasMoreParti(diskDevPath, partitionId):
        for i in range(partitionId + 1, partitionId + 10):
            if os.path.exists(PartiUtil.diskToParti(diskDevPath, i)):
                return True
        return False

    @staticmethod
    def partiExists(partitionDevPath):
        return os.path.exists(partitionDevPath)


print(mbrGetDiskAndPartitionWinBcdUuid("/home/fpemud/test10/stage4-mswin_7/partition0.loop"))
